[MFC] CFile读写文件实现(高效)

您所在的位置:网站首页 mfc findfile [MFC] CFile读写文件实现(高效)

[MFC] CFile读写文件实现(高效)

2023-09-16 03:48| 来源: 网络整理| 查看: 265

1.文件写入

void CMFCApplication1Dlg::Write() { CFile file; CString FileName = "D:\\100w.txt"; memset(buf, 0, NUM);//初始化内存,防止读出字符末尾出现乱码 for (int i = 0; i < NUM; i++) { if ((i / 2000)%2 == 0) buf[i] =10000; else buf[i] = 0; } // TODO: Add your control notification handler code here int aa = buf[NUM/2]; try { file.Open(FileName, CFile::modeCreate | CFile::modeWrite|CFile::typeBinary); //CArchive ar(&file, CArchive::store);//根据打开的文件,创建文件串行化对象 //Serialize(ar); //写文件内容 //ar.Write(buf, NUM); ////结束后关闭对象 //ar.Close(); file.SeekToBegin(); int len = NUM*sizeof(short int); file.Write(buf, NUM*sizeof(short int));//CString m_data //file.Flush(); file.Close(); MessageBox("写入成功!"); } catch (CFileException *e) { CString str; str.Format("写入失败的原因是:%d", e->m_cause); MessageBox("str"); file.Abort(); e->Delete(); } }

2.文件读取

void CMFCApplication1Dlg::Read() { CFile file; CString FileName = "D:\\data.txt"; short int buf[100];//读1K memset(buf, 0, 100*sizeof(short int));//初始化内存,防止读出字符末尾出现乱码 try { if (!file.Open(FileName, CFile::modeRead)) { MessageBox("没有文件!"); return; } file.Seek(10000 * sizeof(short int), CFile::begin); //结构体格式读取 //DATAS aa; //file.Read(&aa, sizeof(aa)); file.Read(buf, sizeof(buf)); file.Close(); MessageBox("读出成功!"); } catch (CFileException *e) { CString str; str.Format("读取数据失败的原因是:%d", e->m_cause); MessageBox("str"); file.Abort(); e->Delete(); } }

3.文件的查找

  当对一个文件操作时,如果不知道该文件是否存在,就要首先进行查找。MFC中有一个专门用来进行文件查找的类CFileFind,使用它可以方便快捷地进行文件的查找。下面这段代码演示了这个类的最基本使用方法。

  CString strFileTitle;   CFileFind finder;   BOOL bWorking = finder.FindFile("C:\\windows\\sysbkup\\*.cab");

4.项目用到的分页读取

读:

void CWave::ShowByPaging_W(CString strPath, int nPageNum, bool bIsShowI, bool bIsShowQ) { CFile file; int nReadDataCount = 0; memset(data_W, 0, nDataNumOfPage_W*2 * sizeof(short int)); try { if (!file.Open(strPath, CFile::modeRead)) { MessageBox("没有文件!"); return; } file.Seek((nPageNum-1) *nDataNumOfPage_W*2*sizeof(short int), CFile::begin); //返回实际读到参数。若读出数据小于20W,则表示已到末尾;此时读的是字节数 int nReadChar = file.Read(data_W, sizeof(short int)*2*nDataNumOfPage_W); nReadDataCount = nReadChar / 2 / sizeof(short int); //file.Read(buf, sizeof(buf)); file.Close(); } catch (CFileException *e) { //CString str; //str.Format("读取数据失败的原因是:%d", e->m_cause); //MessageBox("str"); file.Abort(); e->Delete(); } int startLoc = nDataNumOfPage_W*(m_nCurPageNum_W-1); int count = 0; for (int i = 0; i m_cause); //MessageBox("str"); file.Abort(); e->Delete(); return false; } return false; }

1. 文件模式标志 说明

  CFile::modeCreate  创建方式打开文件,如文件已存在则将其长度设置为0

  CFile::modeNoInherit  不允许继承

  CFile::modeNoTruncate 创建文件时如文件已存在不对其进行截断

  CFile::modeRead 只读方式打开文件

  CFile::modeReadWrite 读写方式打开文件

  CFile::modeWrite 写入方式打开文件

  CFile::shareCompat 在使用过程中允许其他进程同时打开文件

  CFile::shareDenyNone 在使用过程中允许其他进程对文件进行读写

  CFile::shareDenyRead 在使用过程中不允许其他进程对文件进行读取

  CFile::shareDenyWrite 在使用过程中不允许其他进程对文件进行写入

  CFile::shareExclusive  取消对其他进程的所有访问

  CFile::typeBinary 设置文件为二进制模式

  CFile::typeText 设置文件为文本模式

 

参考链接:

http://www.jizhuomi.com/software/234.html

http://www.jizhuomi.com/software/497.html

http://blog.sina.com.cn/s/blog_69b9bb050100lelo.html

http://blog.csdn.net/iscassucess/article/details/8210069

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3